package com.bookshop.user.logic;


import java.util.ArrayList;

import com.bookshop.book.logic.Book;
import com.bookshop.order.logic.OrderManagement;
import com.bookshop.promotion.data.PersonalPromotionMng;
import com.bookshop.promotion.logic.DiscountCoupons;
import com.bookshop.promotion.logic.EqualCoupons;
import com.bookshop.promotion.logic.PersonalPromotion;
import com.bookshop.sale.logic.CollectionManage;
import com.bookshop.vo.OrdersVO;

public class CustomerInfo implements UserInfo{
	private static final long serialVersionUID = 391190224946174213L;
	String mAccount;
	String mName;
	String mCellNumber;
	String mAddress;
	Book[] mCollectionBooks;
	PersonalPromotion mPersonalPromotionInfo;
	
	//SaleService mSaleService;
	CollectionManage mCollectionManage;
	PersonalPromotionMng mPromotionMng;
	
	public CustomerInfo(String pAccount) {
		mAccount=pAccount;
		mName="";
		mCellNumber="";
		mAddress="";
		mCollectionBooks=new Book[0];
		mPersonalPromotionInfo=new PersonalPromotion(0, new EqualCoupons[0], new DiscountCoupons[0]);
	}
	
	public CustomerInfo(String pAccount,String pName,String pCellNumber,String pAddress,Book[] pCollectionBooks,PersonalPromotion pPersonalPromotionInfo){
		mAccount=pAccount;
		mName = pName;
		mCellNumber = pCellNumber;
		mAddress = pAddress;
		mCollectionBooks=pCollectionBooks;
		mPersonalPromotionInfo=pPersonalPromotionInfo;

	}
	
	public void setPersonalPromotion(PersonalPromotion pPersonalPromotionInfo){
		mPromotionMng =new PersonalPromotionMng(mAccount);
		mPersonalPromotionInfo = pPersonalPromotionInfo;
		mPromotionMng.modifyCreditPoints(pPersonalPromotionInfo.getCreditPoints());
//		mPromotionMng.modifyEqualCoupons(pPersonalPromotionInfo.getEqualCoupons());
//		mPromotionMng.modifyDiscountCoupons(pPersonalPromotionInfo.getDiscountCoupon());
		
	}
	
	public boolean collectBooks(ArrayList<Book> pCollectionBooks){
		boolean[] isSuccess=new boolean[pCollectionBooks.size()];
		for (int i = 0; i < pCollectionBooks.size(); i++) {
			isSuccess[i]=collectOne(pCollectionBooks.get(i));
		}
		return result(isSuccess);
	}
	
	public boolean removeBooks(ArrayList<Book> pCollectionBooks){
		boolean[] isSuccess=new boolean[pCollectionBooks.size()];
		for (int i = 0; i < pCollectionBooks.size(); i++) {
			isSuccess[i]=removeOne(pCollectionBooks.get(i));
		}
		return result(isSuccess);
	}
	
	public boolean collectOne(Book pBook){
		mCollectionManage=new CollectionManage(this);
		boolean r=mCollectionManage.add(pBook);
		mCollectionBooks=mCollectionManage.getupdatedBooks();
		return r;
	}
	
	public boolean removeOne(Book pBook){
		 mCollectionManage=new CollectionManage(this);
		 boolean r=mCollectionManage.remove(pBook);
		 mCollectionBooks=mCollectionManage.getupdatedBooks();
		 return r;
	}

	
	public boolean useEqualCoupons(EqualCoupons[] pCoupons){
		mPromotionMng=new PersonalPromotionMng(mAccount);
		boolean r=mPromotionMng.removeEqualCoupons(pCoupons);
		
		mPersonalPromotionInfo.setEuqalCoupons(mPromotionMng.getUpdatedEqualCoupons());
		return r;	
	}
	
	public boolean useDiscountCoupons(DiscountCoupons pCoupon){
		mPromotionMng=new PersonalPromotionMng(mAccount);
		boolean r = mPromotionMng.reduceOneDiscount(pCoupon);
		mPersonalPromotionInfo.setDiscountCoupons(mPromotionMng.getUpdatedDiscountCoupons());
		return r;
	}
	
	public OrdersVO lookOrder(int pBegin,int pEnd){
		OrderManagement om=new OrderManagement();
		return om.searchByUser(mAccount,pBegin , pEnd);
	}
	
	public void addCoupons(EqualCoupons pEqualCoupons,DiscountCoupons pDiscountCoupons){
		mPromotionMng=new PersonalPromotionMng(mAccount);
		mPromotionMng.addOneEqualCoupon(pEqualCoupons);
		mPromotionMng.addOneDiscCoupon(pDiscountCoupons);
	}
	
	public void getCredits(double newCredits){
		mPromotionMng=new PersonalPromotionMng(mAccount);
		double credits = mPersonalPromotionInfo.getCreditPoints();
		mPromotionMng.modifyCreditPoints(credits + newCredits);
	}
	
	public void shrinkCredits(){
		mPromotionMng = new PersonalPromotionMng(mAccount);
		mPromotionMng.shrinkCredits();
	}
	public String getAccount(){
		return mAccount;
	}
	public String getName(){
		return mName;
	}
	public String getCellNumber(){
		return mCellNumber;
	}
	public String getAddress(){
		return mAddress;
	}
	public Book[] getCollectionBooks(){
		return mCollectionBooks;
	}
	public PersonalPromotion getPromotionInfo(){
		return mPersonalPromotionInfo;
	}
	
	private boolean result(boolean[] pIsSuccess){
		for (int i = 0; i < pIsSuccess.length; i++) {
			if(!pIsSuccess[i])
				return false;
		}
		return true;
	}
}
